
COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
   lpstr,rho_hi,rho_h2,rho_star

;; Since we are using routines buried in BGPS_FFORE, compile now to
;; they are available.
FORWARD_FUNCTION BGPS_FFORE_MODEL, BGPS_FFORE_LOOPIE
RESOLVE_ROUTINE, 'BGPS_FFORE', /COMPILE_FULL_FILE, /EITHER

restore,'./irdc_dist_model/bgps_rb3.sav',/ver
restore,'./irdc_dist_model/training_set.sav',/ver


nt = n_elements(training)
n = n_elements(rb3)
trb3 = replicate( create_struct( 'cnum',0L,'kdn',0.,'kdf',0.,'d',0.d,$
                                 'td',0.,'tdm',0.,'tdp',0.,'use',0b), n)

galactic_params
rotc=0b
d = dindgen(!MW.NBINS*5.)*!MW.BINSIZE + !MW.BINSTART
;; Define arrays for TEMP_DEPENDENT_VARIABLES
tau     = dblarr(n)
f_data  = dblarr(n)
sig_f   = dblarr(n)
nearfar = bytarr(n)
thisnf  = bytarr(n)
fitdist = fltarr(n)


message,'Setting the stage (re-used calculations)...',/inf
;; Do the calculations needed by every loop and place them into a
;; structure to save on computation time
GENERATE_LPSTR,ROTC=0b


trb3.kdn = lpstr.kdn
trb3.kdf = lpstr.kdf
trb3.cnum = rb3.cnum

dfn = './irdc_dist_model/data/case5_irdc_TdFIT.sav'
restore,dfn,/ver
do_Tfit = 1b
residj = BGPS_FFORE_LOOPIE( res, NEARFAR=thisnf , FFORE_VEC=ffore_vec)
message,'Nfar = '+string(total(thisnf),format="(I0)"),/inf
message,'Total(RESID): = '+string(total(residj),format="(F0.2)"),/inf
far = thisnf & near = ~ thisnf
fitdist = near * lpstr.kdn + far * lpstr.kdf

ind = where(thisnf NE 0, nnz) & print,nnz
print,res
;; FOR j=0,n-1 DO $
;;    IF nearfar[j] NE thisnf[j] THEN $
;;       print,rb3[j].cnum,nearfar[j],thisnf[j]
   


trb3.d = fitdist


FOR i=0L, nt-1 DO BEGIN
   
   ;; Select by distance
   gcirc,2,training[i].l,training[i].b,rb3.l,rb3.b,dis
   delv = abs(rb3.vlsr-training[i].vlsr)
   
   
   ind = WHERE(dis LE 0.25*3600. AND delv LE 10., nrt)
   print,training[i].name, nrt
   
   IF nrt NE 0 THEN BEGIN
      trb3[ind].use = 1b
      trb3[ind].td  = training[i].d
      trb3[ind].tdm = training[i].dm
      trb3[ind].tdp = training[i].dp
   ENDIF
   
ENDFOR

ind = WHERE(trb3.use NE 0, nuse)
print,nuse

trb3 = trb3[ind]
print,thisnf[ind]
print,rb3[ind].cnum

;; FOR j=0L,nuse-1 DO print,trb3[j]

myps,'./irdc_dist_model/analysis_plots/training_verification.eps',xsize=5

range=[2,16]

plot,trb3.td,trb3.d/1.d3,/isotropic,xr=range,yr=range,/nodata,/xst,/yst,$
     xtit='Maser Parallax Distance [kpc]',ytit='IRDC Modeling Distance [kpc]'



;; oploterror,trb3.td,trb3.kdf/1.d3,trb3.tdm,trb3.td*0.,/lobar,$
;;            psym=8,color=cgColor('Pink'),errcolor=cgColor('Light Gray')
;; oploterror,trb3.td,trb3.kdf/1.d3,trb3.tdp,trb3.td*0.,/hibar,$
;;            psym=8,color=cgColor('Pink'),errcolor=cgColor('Light Gray')
;; oplot,trb3.td,trb3.kdf/1.d3,psym=8,color=cgColor('Pink')


oploterror,trb3.td,trb3.d/1.d3,trb3.tdm,trb3.td*0.,/lobar,$
           psym=3,color=cgColor('Dark Green'),errcolor=cgColor('Slate Gray')
oploterror,trb3.td,trb3.d/1.d3,trb3.tdp,trb3.td*0.,/hibar,$
           psym=3,color=cgColor('Dark Green'),errcolor=cgColor('Slate Gray')

plotsym,0,0.5,/fill
oplot,trb3.td,trb3.d/1.d3,psym=8,color=cgColor('Dark Green')


plots,[!x.crange[0],!y.crange[0]]
plots,[!x.crange[1],!y.crange[1]],/cont,linestyle=3

myps,/done

END
